package com.padakeji.android.ui.examples;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.padakeji.android.ui.widget.RecyclerViewX;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by atanl on 2015/10/22.
 */
public class RecyclerViewXTestActivity extends Activity {

    private RecyclerViewX itemsRecycler;
    private MyRecyclerAdapter mAdapter;
    private List<String> data;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view_test);

        findViews();

        data = Arrays.asList(new String[]{"line1", "line2", "line3", "line4"});

        mAdapter = new MyRecyclerAdapter(this);
        itemsRecycler.setAdapter(mAdapter);

        RecyclerView.LayoutManager linearLayoutManager = new LinearLayoutManager(this);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        itemsRecycler.setLayoutManager(gridLayoutManager);

        View hv = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null, false);
        TextView ht = (TextView) hv.findViewById(android.R.id.text1);
        ht.setText("head1");
        hv.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
        View fv = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null, false);
        TextView ft = (TextView) fv.findViewById(android.R.id.text1);
        ft.setText("footer");
        fv.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
        final View fv1 = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null, false);
        TextView ft1 = (TextView) fv1.findViewById(android.R.id.text1);
        ft1.setText("footer1");
        ft1.setTextColor(getResources().getColor(android.R.color.black));
        fv1.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));
        final View fv2 = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, null, false);
        TextView ft2 = (TextView) fv2.findViewById(android.R.id.text1);
        ft2.setText("footer2");
        ft2.setTextColor(getResources().getColor(android.R.color.black));
        fv2.setBackgroundColor(getResources().getColor(android.R.color.holo_purple));

        hv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        itemsRecycler.removeHeaderView(v);
                    }
                }, 0);
            }
        });

        itemsRecycler.addHeaderView(hv);
        itemsRecycler.addFooterView(fv);
        itemsRecycler.addFooterView(fv1);
        fv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                itemsRecycler.removeFooterView(fv1);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        itemsRecycler.addFooterView(fv2);
                    }
                }, 3000);
            }
        });


    }

    private void findViews() {
        itemsRecycler = (RecyclerViewX) findViewById(R.id.activity_recyclerViewTest_recycler_items);
    }

    private class MyRecyclerAdapter extends RecyclerViewX.Adapter {

        public MyRecyclerAdapter(Context context) {
            super(context);
        }

        @Override
        public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
            View v = getLayoutInflater().inflate(android.R.layout.simple_list_item_1, parent, false);
            v.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
            MyViewHolder vh = new MyViewHolder(v);
            vh.t = (TextView) v.findViewById(android.R.id.text1);
            return vh;
        }

        @Override
        public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
            ((MyViewHolder)holder).t.setText(data.get(position));
        }

        @Override
        public int itemCount() {
            return data.size();
        }
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView t;
        public MyViewHolder(View itemView) {
            super(itemView);
        }
    }

}
